package cn.tedu;

import org.junit.Test;

import java.sql.*;

public class Demo03 {
    @Test //选择 jUnit4
    public void insert() throws SQLException {
        System.out.println("insert");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/empdb?characterEncoding=utf8&serverTimezone=Asia/Shanghai",
                "root","root");
        Statement s = conn.createStatement();
        s.execute("insert into emp(name) values('Tom')");
        conn.close();
    }
    @Test
    public void  delete() throws SQLException {
        System.out.println("delete");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/empdb?characterEncoding=utf8&serverTimezone=Asia/Shanghai",
                "root","root");
        Statement s = conn.createStatement();
        s.execute("delete from emp where name='Jerry'");
        conn.close();
    }
    @Test
    public void update() throws SQLException {
        System.out.println("update");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/empdb?characterEncoding=utf8&serverTimezone=Asia/Shanghai",
                "root","root");
        Statement s = conn.createStatement();
        s.execute("update emp set name='Jerry' where name='Tom'");
        conn.close();
    }
    @Test
    public void select() throws SQLException {
        System.out.println("select");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/empdb?characterEncoding=utf8&serverTimezone=Asia/Shanghai",
                "root","root");
        Statement s = conn.createStatement();
        ResultSet rs =
                s.executeQuery("select name,job,sal from emp");
        //遍历结果 next下一个,让游标下移,返回值表示是否有下一条数据
        while (rs.next()){
            //1.通过字段名获取
            //String nmae =rs.getString("name");
            //String job =rs.getString("job");
            //double sal = rs.getDouble("sal");
            //2.通过字段的位置获取
            String name =rs.getString(1);
            String job =rs.getString(2);
            double sal = rs.getDouble(3);

            System.out.println(name+":"+job+":"+sal);
        }
        conn.close();
    }
}
